package cn.cmdr.controller;

import cn.cmdr.pojo.Journal;
import cn.cmdr.pojo.OrderItem;
import cn.cmdr.pojo.ReturnGoods;
import cn.cmdr.pojo.Staff;
import cn.cmdr.service.JournalService;
import cn.cmdr.service.OrderItemService;
import cn.cmdr.service.OrderService;
import cn.cmdr.service.ReturnsService;
import cn.cmdr.vo.ItemVo;
import cn.cmdr.vo.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ord/apply")
public class ReturnsController {
    @Resource
    private ReturnsService returnsService;
    @Resource
    private OrderService orderService;
    @Resource
    private OrderItemService orderItemService;
    @Resource
    private JournalService journalService;

    @PostMapping("/list/{pageNo}/{pageSize}")
    public ResultVo getlist(@PathVariable Integer pageNo, @PathVariable Integer pageSize,@RequestBody ReturnGoods returnGoods){
        try {
            return ResultVo.sucess(returnsService.getPageReturnList(pageNo,pageSize,returnGoods));
        }catch (Exception e){
            return ResultVo.fail("查询失败",e);  //包含了打印错误日志
        }
    }

    @GetMapping("/view")
    public ModelAndView view(String id,String cid){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("ord/applyview");
        modelAndView.addObject("id",id);
        modelAndView.addObject("orderItem",orderItemService.getOrdItemTwo(Integer.parseInt(id)));
        modelAndView.addObject("returnGoods",returnsService.getReturnGoodsList(Integer.parseInt(cid)));
        return modelAndView;
    }

    @GetMapping("/orderItemList")
    public ResultVo orderItemList(String id, Model model){
        List<OrderItem> list = orderItemService.getOrdItemTwo(Integer.parseInt(id));
        return ResultVo.sucess(new ItemVo(list,list.size()));
    }

    @PostMapping("/okUpdate/{id}")
    @RequiresPermissions("apply:processing")
    public ResultVo okUpdate(@RequestBody ReturnGoods returnGoods,@PathVariable String id){
        try {
            Staff staff =  (Staff) SecurityUtils.getSubject().getPrincipal();
            returnGoods.setHandler(staff.getStaffCode());
            returnGoods.setId(Integer.parseInt(id));
            returnGoods.setProcessingTime(new Date());
            returnGoods.setState(2);
            returnsService.getUpdate(returnGoods);
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("同意退货;备注："+returnGoods.getRemarks());
            journal.setMenuId(5);
            journalService.add(journal);
            return ResultVo.sucess("确认退货成功");
        }catch (Exception e){
            return ResultVo.fail("确认退货失败",e);  //包含了打印错误日志
        }
    }

    @PostMapping("/noUpdate/{id}")
    @RequiresPermissions("apply:processing")
    public ResultVo noUpdate(@RequestBody ReturnGoods returnGoods,@PathVariable String id){
        try {
            Staff staff =  (Staff) SecurityUtils.getSubject().getPrincipal();
            returnGoods.setHandler(staff.getStaffCode());
            returnGoods.setId(Integer.parseInt(id));
            returnGoods.setProcessingTime(new Date());
            returnGoods.setState(0);
            returnsService.getUpdate(returnGoods);
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("拒绝退货;备注："+returnGoods.getRemarks());
            journal.setMenuId(5);
            journalService.add(journal);
            return ResultVo.sucess("拒绝退货成功");
        }catch (Exception e){
            return ResultVo.fail("拒绝退货失败",e);  //包含了打印错误日志
        }
    }

    @PostMapping("/ok/{id}")
    @RequiresPermissions("apply:processing")
    public ResultVo ok(@PathVariable String id,@RequestBody ReturnGoods returnGoods){
        try {
            returnGoods.setId(Integer.parseInt(id));
            returnGoods.setState(3);
            Staff staff =  (Staff) SecurityUtils.getSubject().getPrincipal();
            returnGoods.setConsignee(staff.getStaffCode());
            returnGoods.setReceivingTime(new Date());
            returnsService.getUpdate(returnGoods);
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("确认退货;备注："+returnGoods.getReceivingRemarks());
            journal.setMenuId(5);
            journalService.add(journal);
            return ResultVo.sucess("确认收货成功");
        }catch (Exception e){
            return ResultVo.fail("确认收货失败",e);  //包含了打印错误日志
        }
    }

//    @GetMapping("/pdel")
//    public ResultVo pdel(Integer[] id){
//        try {
//            returnsService.pdel(id);
//            return ResultVo.sucess("删除成功");
//        }catch (Exception e){
//            return ResultVo.fail("删除失败",e);
//        }
//    }

    @PostMapping("/add")
    public ResultVo add(@RequestBody ReturnGoods returnGoods){
        try {
            returnGoods.setState(1);
            returnGoods.setApplicationTime(new Date());
            returnsService.add(returnGoods);
            return ResultVo.sucess("申请成功");
        }catch (Exception e){
            return ResultVo.fail("申请失败",e);
        }
    }

    @PostMapping("/returnGoodsOne")
    public ReturnGoods returnGoodsOne(String orderItem){
        return returnsService.getReturnGoodsOne(Integer.parseInt(orderItem));
    }

    @PostMapping("/update")
    public ResultVo update(@RequestBody ReturnGoods returnGoods){
        try {
            returnGoods.setState(1);
            returnGoods.setApplicationTime(new Date());
            returnsService.getUpdateTwo(returnGoods);
            return ResultVo.sucess("申请成功");
        }catch (Exception e){
            return ResultVo.fail("申请失败",e);
        }
    }

    @PostMapping("/update222")
    public ResultVo update222(@RequestBody ReturnGoods returnGoods){
        try {
            returnGoods.setState(4);
            returnGoods.setApplicationTime(new Date());
            returnsService.getUpdateTwo(returnGoods);
            return ResultVo.sucess("取消退货");
        }catch (Exception e){
            return ResultVo.fail("取消失败",e);
        }
    }

//    @PostMapping("/del")
//    public ResultVo del(String orderItem){
//        try {
//            returnsService.del(Integer.parseInt(orderItem));
//            return ResultVo.sucess("取消退货成功");
//        }catch (Exception e){
//            return ResultVo.fail("取消退货失败",e);
//        }
//    }

}
